package com.slipper.system.collect.service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.system.collect.bo.CollectTableBO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.collect.dao.CollectColumnDao;
import com.slipper.system.collect.bo.CollectColumnBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.Tool;
import com.slipper.util.UUID;

@Service
@ReadTransactional
public class CollectColumnService {
    @Resource
    private CollectColumnDao collectColumnDao;
    
    public CollectColumnBO queryBy(String id) {
        return collectColumnDao.queryBy(id);
    }
    
    public List<CollectColumnBO> queryList(CollectColumnBO bo) {
        return collectColumnDao.queryList(bo);
    }
    
    public PageInfo<CollectColumnBO> queryPage(CollectColumnBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(collectColumnDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(CollectColumnBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            return collectColumnDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            return collectColumnDao.insert(bo);
        }
    }

    @WriteTransactional
    public void save(String tableId, List<CollectColumnBO> columnList, String employeeId) {
        collectColumnDao.deleteByTableId(tableId);
        columnList.forEach(column -> {
            column.setId(UUID.getUUID());
            column.setTableId(tableId);
            column.setUpdateId(employeeId);
            column.setCreateId(employeeId);
            column.setUpdateTime(System.currentTimeMillis());
            column.setCreateTime(System.currentTimeMillis());
            collectColumnDao.insert(column);
        });
    }
    
    @WriteTransactional
    public int delete(CollectColumnBO bo) {
        return collectColumnDao.delete(bo);
    }

    public List<CollectColumnBO> queryByTableIds(String[] tableIds) {
        if (tableIds == null || tableIds.length == 0) return new ArrayList<>();
        return collectColumnDao.queryByTableIds(tableIds);
    }

    @WriteTransactional
    public void deleteByTableId(String tableId) {
        collectColumnDao.deleteByTableId(tableId);
    }

    public void fillColumn(List<CollectTableBO> tableList) {
        String[] tableIds = tableList.stream().map(CollectTableBO::getId).toArray(String[]::new);
        queryByTableIds(tableIds).stream()
                .collect(Collectors.groupingBy(CollectColumnBO::getTableId))
                .forEach((tableId, columnList) -> {
                    List<CollectColumnBO> sortedColumnList = columnList.stream()
                            .sorted(Comparator.comparing(CollectColumnBO::getOriginColumn, Comparator.nullsLast(Comparator.naturalOrder())))
                            .collect(Collectors.toList());
                    tableList.stream()
                            .filter(table -> table.getId().equals(tableId))
                            .findFirst()
                            .ifPresent(table -> table.setColumnList(sortedColumnList));
                });
    }
}
